<!-- 非SAP客户待处理 -->
<template>
<div class="eventLevel">
  <CommonBtn label-name="待处理客户列表" v-if="!showEdit" :buttons="buttons" @onButtonClick="onButtonClick">
    <span slot="conditions" v-for="(item,index) of conditions" :key="index" v-if="item.checked && item.label && item.valueName != undefined">{{item.label+":"+item.valueName}}</span>
  </CommonBtn>
  <CommonList v-if="!showEdit" :buttons="buttons" :columns="columns" :listUrl="$api.API_SMMS_DEALER_LIST" :conditions="conditions" @tableSelect="tableSelect" @dbClick="openItem"></CommonList>
  <CustomerEdit :pageType="pageType" ref="commonEdit" :disabled="!enableEdit" isNoSAPHandle readyDeal :label="editTitle" v-if="showEdit" :items="items" :originalData="row" @editBack="editBack" @getSelectOption="getSelectOption" @search="search" @submit="submit" />
  <CommonSearch :label="item.label" :conditions="item.conditions" v-for="(item, index) of searchs" :key="index" :listUrl="item.listUrl" :columns="item.columns" :originalData="item.searchData" :items="item.items" :page="item.page" @search="search" @submit="item.selectItem" @searchBack="searchBack"/>
  <CommonExport :out-btn="button" :props="columns" :export_type="export_type"
                @close="close" :export-url="$api.API_SMMSDEALER_EXPORT" :conditions="conditions"></CommonExport>
</div>
</template>

<script>
import http from '@/api/http'
import {Message } from 'element-ui'
import CommonList from "../../common/CommonList"
import CommonBtn from "../../common/CommonBtn";
import CustomerEdit from "../customerInfo/CustomerEdit";
import Tools from "@/util/Tools.js";
import CommonSearch from '../../common/CommonSearch'
import CommonExport from "../../common/CommonExport";
export default {
  name: 'smmsDealerPendingList',
  data() {
    return {
      searchData: {},
      searchItems: [],
      searchs: [],
      enableEdit: true,
      buttons: [
        this.$config.BTNS.ADD,
        this.$config.BTNS.EXPORT,
        this.$config.BTNS.SEARCH
      ],
      columns: [{
          prop: 'smmsCode',
          label: '统一编码'
        },
        {
          prop: 'smmsName',
          label: '客户名称'
        },
        {
          prop: 'wfStatus',
          label: '节点状态'
        },
        {
          prop: 'levelName',
          label: '客户属性'
        },
        {
          prop: 'dataSource',
          label: '数据来源'
        },
        {
          prop: 'smmsOwnerName',
          label: '供货方名称'
        },
        {
          prop: 'buName',
          label: '业务单位'
        },
        {
          prop: 'customNameCn',
          label: '客户集团'
        },
        {
          prop:'isLKAvalue',
          label:'是否LKA'
        },
        {
          prop: 'brandName',
          label: '适用品牌'
        },
        {
          prop: 'province',
          label: '省',
          minWidth: 50
        },
        {
          prop: 'physicalCity',
          label: '市',
          minWidth: 50
        },
        {
          prop: 'adminCity',
          label: '区县',
          minWidth: 50
        },
        {
          prop: 'deAddress',
          label: '详细地址'
        },
        {
          prop: 'repName',
          label: '负责人'
        },
        {
          prop: 'createTime',
          label: '创建时间'
        },
        {
          prop: 'modifyTime',
          label: '最后修改时间'
        }
      ],
      items: [{
          name: '统一编码',
          key: 'smmsCode',
          type: 'text',
          disable: true,
          required: true,
          placeholder: '系统自动生成'
        },
        {
          name: '客户属性',
          key: 'levelName',
          vkey: 'fkDealerLevel',
          type: 'single-select',
          required: true,
          disable: true,
          options: []
        },
        {
          name: 'SoldToCode(客户编码)',
          key: 'soldToCode',
          type: 'checktext',
          required: true,
          disable: true,
          placeholder: '',
          checkname: '是否自动生成'
        },
        {
          name: '客户名称',
          key: 'smmsName',
          type: 'text',
          placeholder: '',
          required: true
        },
        {
          name: '地理城市',
          key: 'city',
          type: 'multi-select',
          required: true,
          optionss: [{
            options: [],
            key: 'province'
          }, {
            options: [],
            key: 'physicalCity'
          }, {
            options: [],
            key: 'adminCity'
          }]
        },
        {
          name: '城市类别',
          key: 'cityTypeName',
          vkey: 'fkCityType',
          type: 'single-select',
          required: false,
          disable: true,
          options: []
        },
        {
          name: '详细地址',
          key: 'deAddress',
          type: 'multi-text',
          required: true,
          texts: [{
            key: 'address',
            name: '街道',
            span: 12
          }, {
            key: 'address1',
            name: '路',
            span: 12
          }, {
            key: 'address2',
            name: null,
            span: 24
          }]
        },
        {
          name: '客户负责人',
          key: 'repName',
          type: 'search',
          required: true,
          disable: false
        },
        {
          name: '关联供货方',
          key: 'smmsOwnerName',
          type: 'search',
          required: true,
          disable: false
        },
        {
          name: '供货方代码',
          key: 'smmsSoldToCode',
          type: 'text',
          required: true,
          disable: true
        },
        {
          name: '业务单位',
          key: 'buName',
          vkey: 'fkBu',
          type: 'single-select',
          required: true,
          disable: true,
          options: []
        },
        {
          name: '客户集团',
          key: 'customNameCn',
          type: 'search',
          required: true,
          disable: true
        },
        {
          name: '是否LKA',
          key: 'isLKAvalue',
          vkey: 'isLKA',
          type: 'single-select',
          required: true,
          disable: false,
          placeholder: '请选择',
          options: []
        },
        {
          name: '适用品牌',
          key: 'brandName',
          vkey: 'fkBrand',
          type: 'single-select',
          required: true,
          disable: true,
          options: []
        },
        {
          name: 'Destination',
          key: 'destinationName',
          vkey: 'fkDestination',
          type: 'single-select',
          required: false,
          disable: true,
          options: []
        },
        {
          name: '客户联系人',
          key: 'linkMan',
          type: 'text',
          placeholder: ''
        },
        {
          name: '联系人手机',
          key: 'linkPhone',
          type: 'text',
          placeholder: ''
        },
        {
          name: '邮箱地址',
          key: 'linkEmail',
          type: 'text',
          placeholder: '多个邮箱请用英文,(逗号分割)'
        },
        {
          name: '申请费用状态',
          key: 'tpFeeStatus',
          type: 'single-select',
          required: true,
          disable: false,
          options: []
        },
        {
          name: '客户营业状态',
          key: 'businessStatus',
          type: 'single-select',
          required: true,
          disable: false,
          options: []
        },
        {
          name: '合作状态',
          key: 'cooperationStatus',
          type: 'single-select',
          required: true,
          disable: false,
          options: []
        },
        {
          name: 'TPWF客户编码',
          key: 'tpCode',
          type: 'text',
          required: false,
          disable: true
        },
        {
          name: 'JDES Code',
          key: 'jdesCode',
          type: 'text',
          required: false,
          disable: true
        },
      ],
      conditions: [{
        checked: true,
        prop: 'pageType',
        value: '2'
      }],
        export_type:[{
            checked: true,
            prop: 'pageType',
            value: '2'
        }],
        button:null,
      editTitle: '',
      showEdit: false,
      row: null,
      pageType: 2
    }
  },
  methods: {
    editBack() {
      this.editTitle = ''
      this.row = null
      this.showEdit = false
    },
    onButtonClick(item) {

      if (item.event == 'edit()') {
          this.enableEdit = true

        if (!this.row || JSON.stringify(this.row) == '{}') {
          this.$message.error("请选择一行数据")
          return
        }
          this.showEdit = true
        this.editTitle = '待处理客户信息'
      }

      if (item.event == 'add()') {
          this.enableEdit = true
          this.showEdit = true
        this.editTitle = '新增客户信息'
        this.row = null
        this.items[1].disable = false
        this.items[2].disable = false
      }
      let self = this
      if (item.event == 'search()') {
        this.searchs.push({
          label: '查询客户信息',
          page: 'customer',
          searchData: this.searchData,
          items: this.searchItems,
          selectItem: function (formData, items) {
            self.searchs.pop()
            self.searchItems = items;
            self.searchData = formData;
            let data = {}
            let conditions = self.conditions.filter(item=>{
              return !item.label
            })
            // 回调
            for (let idx in items) {
              let item = items[idx]
              if (item.checked == true) {
                if (item.vkey) {
                  data[item.vkey] = formData[item.vkey]
                } else {
                  data[item.key] = formData[item.key]
                }
                conditions.push({
                  checked: true,
                  label: item.name,
                  prop: item.vkey ? item.vkey : item.key,
                  valueName: formData[item.key],
                  value: item.vkey ? formData[item.vkey] : formData[item.key]
                })
              }
            }
            self.conditions = conditions
          },
        })
      }
        this.button = item
    },
    tableSelect(item) {
      this.row = item
    },
    getSelectOption(item, data) {
      console.log(item)
      if (item.key == "levelName") {
        Tools.getDealerLevels({pkid: data ? data.pkid:null, pageType: 2},data=>{
          data.forEach(item => {
            item.name = item.levelName
            item.value = item.pkid
          })
          item.options = data
        })
      } else if (item.key == "businessStatus") {
        Tools.getBussinessStatus(data=>{
          data.forEach(item => {
            item.name = item.dvalue
            item.value = item.dkey
          })
          item.options = data
        })
      } else if (item.key == "cooperationStatus") {
        Tools.getCooperationStatus(data=>{
          data.forEach(item => {
            item.name = item.dvalue
            item.value = item.dkey
          })
          item.options = data
        })
      } else if (item.key == "tpFeeStatus") {
        Tools.getFeeStatus(data=>{
          data.forEach(item => {
            item.name = item.dvalue
            item.value = item.dkey
          })
          item.options = data
        })
      } else if (item.key == "isLKAvalue") {
        Tools.getisLkaList(data=>{
          data.forEach(item => {
            item.name = item.isLKAvalue
            item.value = item.isLKA
          })
          item.options = data
        })
      } else if (item.key == "province") {
        // 地理城市
        Tools.getProvince(data => {
          data.forEach(item => {
            item.name = item.provinceName
            item.value = item.provinceName
          })
          item.options = data
        })
      } else if (item.key == "physicalCity") {
        // 地理城市
        Tools.getCity({provinceName: data.province},data => {
          data.forEach(item => {
            item.name = item.cityName
            item.value = item.cityName
          })
          item.options = data
        })
      } else if (item.key == "adminCity") {
        // 地理城市
        Tools.getAdminCity({provinceName: data.province, cityName: data.physicalCity},data => {
          console.log(data)
          data.forEach(item => {
            item.name = item.adminCity
            item.value = item.adminCity
            item.fkAdminCity = item.pkid
            item.fkCityType = item.fkCityType
          })
          item.options = data
        })
      }
    },
    search(item, callback) {
      let self = this
      if (item.key == 'smmsOwnerName') {
        // 关联供货方
        this.searchs.push({
          label: '直供客户信息',
          conditions: [{checked: true, prop: 'pageType', value: 2},{checked: true, prop: 'dealerLevelId', value: this.$refs.commonEdit.formData.fkDealerLevel}],
          listUrl: this.$api.API_SMMS_DEALER_OWNER_LIST,
          selectItem: function (item) {
            self.searchs.pop()
            // 回调
            if (typeof callback == 'function') {
              callback({smmsOwnerName: item.smmsName,
                        smmsSoldToCode: item.soldToCode,
                        smmsOwnerCode: item.smmsCode,
                        fkBu: item.fkBu,
                        buName: item.buName,
                        fkBrand: item.fkBrand,
                        brandName: item.brandName,
                        fkOwner: item.pkid})
            }
          },
          items: [{
            name: '客户名称',
            key: 'smmsName',
            type: 'text',
          },{
            name: 'SoldToCode',
            key: 'soldToCode',
            type: 'text',
          },{
            name: '统一编码',
            key: 'smmsCode',
            type: 'text',
          },{
            name: '客户属性',
            key: 'levelName',
            vkey: 'fkDealerLevel',
            type: 'search',
          },{
            name: '客户集团',
            key: 'customNameCn',
            vkey: 'fkCustom',
            type: 'search',
          },{
            name: '客户负责人',
            key: 'repName',
            vkey: 'fkRep',
            type: 'search',
          }],
          columns: [{
            prop: 'smmsName',
            label: '客户名称'
          },{
            prop: 'soldToCode',
            label: 'SoldToCode'
          },{
            prop: 'smmsCode',
            label: '统一编码'
          },{
            prop: 'levelName',
            label: '客户属性'
          },{
            prop: 'customNameCn',
            label: '客户集团'
          },{
            prop: 'repName',
            label: '客户负责人'
          }]
        })
      } else if (item.key == 'levelName') {
        // 客户属性
        this.searchs.push({
          label: '客户属性管理',
          conditions: [],
          listUrl: this.$api.API_CUSTOMERTYPE,
          selectItem: function (item) {
            self.searchs.pop()
            // 回调
            if (typeof(callback) == 'function') {
              callback({levelName: item.levelName, fkDealerLevel: item.pkid})
            }
          },
          items: [{
            name: '客户属性名称',
            key: 'levelName',
            type: 'text',
          },{
            name: '客户属性代码',
            key: 'levelCode',
            type: 'text',
          },{
            name: '是否有效',
            key: 'isValid',
            type: 'checkbox',
          }],
          columns: [{
            prop: 'levelName',
            label: '客户属性名称'
          },{
            prop: 'levelCode',
            label: '客户属性代码'
          },{
            prop: 'valid',
            label: '是否有效'
          }]
        })
      } else if (item.key == 'repName') {
        // 客户属性
        this.searchs.push({
          label: '人员信息',
          conditions: [],
          listUrl: this.$api.API_GETTPINFO_LIST,
          selectItem: function (item) {
            self.searchs.pop()
            // 回调
            if (typeof(callback) == 'function') {
              callback({repName: item.repName, fkRep: item.pkid})
            }
          },
          items: [{
            name: '员工姓名',
            key: 'repName',
            type: 'text',
          },{
            name: '员工账号',
            key: 'repCode',
            type: 'text',
          },{
            name: '所属管理层级',
            key: 'positionName',
            type: 'search',
          },{
            name: 'WWID',
            key: 'wWID',
            type: 'text',
          },{
            name: 'SAP代码',
            key: 'sapCode',
            type: 'text',
          },{
            name: '总部/大区',
            key: 'hQOrRegion',
            type: 'text',
          }],
          columns: [{
            prop: 'repName',
            label: '员工姓名'
          },{
            prop: 'repCode',
            label: '员工账号'
          },{
            prop: 'positionName',
            label: '所属管理层级'
          },{
            prop: 'WWID',
            label: 'wWID'
          },{
            prop: 'sapCode',
            label: 'SAP代码'
          },{
            prop: 'hQOrRegion',
            label: '总部/大区'
          }]
        })
      } else if (item.key == 'customNameCn') {
        // 客户集团
        this.searchs.push({
          label: '客户集团',
          conditions: [],
          listUrl: this.$api.API_SELECT_CUSTOMER_GROUP_INFO,
          selectItem: function (item) {
            self.searchs.pop()
            // 回调
            if (typeof(callback) == 'function') {
              callback({customNameCn: item.customNameCN, fkCustom: item.pkid})
            }
          },
          items: [{
            name: '集团中文名称',
            key: 'customNameCN',
            type: 'text',
          },{
            name: 'Destination',
            key: 'destinationName',
            type: 'text',
          },{
            name: '集团英文名称',
            key: 'customNameEN',
            type: 'text',
          },{
            name: '预算Account',
            key: 'accountCode',
            type: 'text',
          },{
            name: '客户集团分类',
            key: 'customTypeName',
            type: 'search',
          },{
            name: '是否有效',
            key: 'isValid',
            type: 'checkbox',
          }],
          columns: [{
            prop: 'customNameCN',
            label: '集团中文名称'
          },{
            prop: 'destinationName',
            label: 'Destination'
          },{
            prop: 'customNameEN',
            label: '集团英文名称'
          },{
            prop: 'accountCode',
            label: '预算Account'
          },{
            prop: 'customTypeName',
            label: '客户集团分类'
          },{
            prop: 'valid',
            label: '是否有效'
          }]
        })
      } else if (item.key == 'positionName') {
        // 职位信息
        this.searchs.push({
          label: '职位信息',
          conditions: [],
          listUrl: this.$api.API_MANAGEMENTHIERARCHY_LIST,
          selectItem: function (item) {
            self.searchs.pop()
            // 回调
            if (typeof(callback) == 'function') {
              callback({positionName: item.positionName, fkPosition: item.pkid})
            }
          },
          items: [{
            name: '职位名称',
            key: 'positionName',
            type: 'text',
          },{
            name: '职位代码',
            key: 'positionCode',
            type: 'text',
          }],
          columns: [{
            prop: 'positionName',
            label: '职位名称'
          },{
            prop: 'createTime',
            label: '创建时间'
          },{
            prop: 'modifyTime',
            label: '最后修改时间'
          },{
            prop: 'isValid',
            label: '是否有效'
          },{
            prop: 'positionCode',
            label: '职位代码'
          }]
        })
      } else if (item.key == 'customTypeName') {
        // 客户集团分类
        this.searchs.push({
          label: '客户集团分类',
          conditions: [],
          listUrl: this.$api.API_CUSTOMERCLASSIFY_LIST,
          selectItem: function (item) {
            self.searchs.pop()
            // 回调
            if (typeof(callback) == 'function') {
              callback({customTypeName: item.customTypeName, fkCustomType: item.pkid})
            }
          },
          items: [{
            name: '集团类型编码',
            key: 'customTypeCode',
            type: 'text',
          },{
            name: '集团类型名称',
            key: 'customTypeName',
            type: 'text',
          },{
            name: '是否有效',
            key: 'isValid',
            type: 'checkbox',
          }],
          columns: [{
            prop: 'customTypeCode',
            label: '集团类型编码'
          },{
            prop: 'customTypeName',
            label: '集团类型名称'
          },{
            prop: 'valid',
            label: '是否有效'
          }]
        })
      } else if (item.key == 'payLinkCode') {
        // PayLink编码
        this.searchs.push({
          label: '',
          conditions: [],
          listUrl: this.$api.API_STORE_PAYLINK_LIST,
          selectItem: function (item) {
            self.searchs.pop()
            // 回调
            if (typeof(callback) == 'function') {
              callback({payLinkCode: item.payLinkCode, payLinkName: item.payLinkName, fkPayLink: item.pkid})
            }
          },
          items: [{
            name: 'PayLink编码',
            key: 'payLinkCode',
            type: 'text',
          },{
            name: 'PayLink名称',
            key: 'payLinkName',
            type: 'text',
          }],
          columns: [{
            prop: 'payLinkCode',
            label: 'PayLink编码'
          },{
            prop: 'payLinkName',
            label: 'PayLink名称'
          }]
        })
      }
    },
    submit(item) {
      console.log(item)
      item.pageType = this.pageType
      if (this.row) {
        // 编辑
        http.post(this.$api.API_SMMS_UPDATE_LIST, item)
            .then(resp=>{
          Message({type: 'success',message: '保存成功'});
          this.showEdit = false
        })
      } else {
        // 新增
        http.post(this.$api.API_SMMS_SAVE_LIST, item)
        .then(resp=>{
          Message({type: 'success',message: '保存成功'});
          this.showEdit = false
        })
      }
    },
    openItem(item) {
      // 打开详情
      this.showEdit = true
      this.enableEdit = true
      this.items[1].disable = false
      this.items[2].disable = true
      this.editTitle = '待处理客户信息'
    },
    searchBack() {
      this.searchs.pop()
    },
      close(item) {
          this.button = item
      }
  },
  watch: {
    showEdit(newVal,oldVal) {
      if (!newVal && oldVal) {
        this.conditions = this.conditions.map(item=>{return item})
      }
    }
  },
  components: {
      CommonExport,
      CommonList,
      CommonBtn,
      CustomerEdit,
      CommonSearch
  }
}
</script>
